function MainMapInputHandler(){
	"use strict";
	//
	// Add listeners to handle canvas mouse events: Drag and drop
	var auxMainMapCanvas = document.getElementById("mainMapCanvasId");
	auxMainMapCanvas.onmousedown = MainMapInputHandler.MainMapCanvas_onmousedown;
	auxMainMapCanvas.onmouseup = MainMapInputHandler.MainMapCanvas_onmouseup;
	auxMainMapCanvas.onmousemove = null;
	//
	// Add listeners to handle canvas mouse click events
	auxMainMapCanvas.onclick          = MainMapInputHandler.MainMapCanvas_onclick;
	auxMainMapCanvas.ondoubleclick    = MainMapInputHandler.MainMapCanvas_ondoubleclick;
	//
	// Add listeners to handle canvas mouse over events
	auxMainMapCanvas.onmouseover      = MainMapInputHandler.MainMapCanvas_onmouseover; 
	auxMainMapCanvas.onmouseout       = MainMapInputHandler.MainMapCanvas_onmouseout; 
	//
	// Disable events on canvas
	auxMainMapCanvas.onmousewheel     = function(evt){return false;}; 
	auxMainMapCanvas.oncontextmenu    = MainMapInputHandler.MainMapCanvas_oncontextmenu; 
	//
	// Attributes
	// MainMapInputHandler.hadDraggedAndDroped: true if has not moved since mouse down
	//
	// Used but initialized before usage
	// MainMapInputHandler.MainMapCanvas_initialEventX
	// MainMapInputHandler.MainMapCanvas_initialEventY
	// MainMapInputHandler.MainMapCanvas_initialCammeraX
	// MainMapInputHandler.MainMapCanvas_initialCammeraY
}

// Handles mouse movement (drag and drop) on main canvas
MainMapInputHandler.MainMapCanvas_onmousemove = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	if (EventsUtil.isLeftButtonPressed(event)){
		//
		// computes offset
		var offx = MainMapInputHandler.MainMapCanvas_initialEventX - event.pageX;
		var offy = MainMapInputHandler.MainMapCanvas_initialEventY - event.pageY;
		//
		// Marks D&D (therefore, not click) 
		if (Math.abs(offx)>5 ||Math.abs(offy)>5){
			MainMapInputHandler.hadDraggedAndDroped = true;
		}
		//
		// updates position
		var newCammeraX = MainMapInputHandler.MainMapCanvas_initialCammeraX + offx;
		var newCammeraY = MainMapInputHandler.MainMapCanvas_initialCammeraY + offy;
		//
		// Delegates movement to cammera for updating
		Game.gui.cammera.updateCammeraCoordinates(newCammeraX, newCammeraY);
	}
	//
	event.stopPropagation();        
	return false;
};

// Handles mousedown on main canvas
MainMapInputHandler.MainMapCanvas_onmousedown = function (e) {
	"use strict";
	var event, auxMainMapCanvas;
	event = EventsUtil.getEvent(e);
	//
	if (EventsUtil.isLeftButtonPressed(event)){
		//
		//Resets D&D detection
		MainMapInputHandler.hadDraggedAndDroped = false;
		//
		// Stores initial position
		MainMapInputHandler.MainMapCanvas_initialEventX = event.pageX;
		MainMapInputHandler.MainMapCanvas_initialEventY = event.pageY;
		MainMapInputHandler.MainMapCanvas_initialCammeraX = Game.gui.cammera.x;
		MainMapInputHandler.MainMapCanvas_initialCammeraY = Game.gui.cammera.y;
		//
		// Set movement listener
		auxMainMapCanvas = document.getElementById("mainMapCanvasId");
		auxMainMapCanvas.onmousemove = MainMapInputHandler.MainMapCanvas_onmousemove;
	}
	//
	event.stopPropagation();        
	return false;
};

// Handles mouseup on main canvas
MainMapInputHandler.MainMapCanvas_onmouseup = function (e) {
	"use strict";
	var event, auxMainMapCanvas;
	event = EventsUtil.getEvent(e);
	//
	if (EventsUtil.isLeftButtonPressed(event)){
		//
		// Remove movement listener
		auxMainMapCanvas = document.getElementById("mainMapCanvasId");
		auxMainMapCanvas.onmousemove = null;
	}
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse click on main canvas
MainMapInputHandler.MainMapCanvas_onclick = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	// if it was not a drag and drop
	if (!MainMapInputHandler.hadDraggedAndDroped){
		//
		// Processes left click
		MainMapInputHandler.MainMapCanvas_handleLeftMouseClick(event);
		//
	}
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse double click on main canvas
MainMapInputHandler.MainMapCanvas_ondoubleclick = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse over on main canvas
MainMapInputHandler.MainMapCanvas_onmouseover = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse out on main canvas
MainMapInputHandler.MainMapCanvas_onmouseout = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse right click on main canvas
MainMapInputHandler.MainMapCanvas_oncontextmenu = function (e) {
	"use strict";
	var event = EventsUtil.getEvent(e);
	//
	// Processes right click
	MainMapInputHandler.MainMapCanvas_handleRightMouseClick(event);
	//
	event.stopPropagation();        
	return false;
};

// Handles mouse left click on main canvas (Selection)
MainMapInputHandler.MainMapCanvas_handleLeftMouseClick = function(e){
	"use strict";
	var xm, ym, object;
	//
	// Compute coordintates taking into account mouse and cammera position
	xm = e.offsetX + Game.gui.cammera.x;
	ym = e.offsetY + Game.gui.cammera.y;
	//
	// Finds closest object
	object = Game.model.objectBag.findClosestObject(xm,ym);
	//
	// If CTRL is not pressed, clear list of selected objects
    if (KeyboardInputHandler.keysPressed.hasOwnProperty(Keys.CTRL) && KeyboardInputHandler.keysPressed[Keys.CTRL]){
	} else {
		Game.gui.clearSelectedObjects();
	}
	//
	// Add selected object to the list or remove it depending on whether it was selected or not.
	if (object) {
		if (object.isSelected()){
			Game.gui.removeSelectedObject(object);
		}else{
			Game.gui.addSelectedObject(object);
		}
	}
	//
};

// Handles mouse right click on main canvas (Go to)
MainMapInputHandler.MainMapCanvas_handleRightMouseClick = function(e){
	"use strict";
	var xm, ym, tx, ty, i;
	//
	// Compute coordintates taking into account mouse and cammera position
	xm = e.offsetX + Game.gui.cammera.x;
	ym = e.offsetY + Game.gui.cammera.y;
	//
	tx = CoordinatesUtil.getTileX(xm,ym);
	ty = CoordinatesUtil.getTileY(xm,ym);
	//
	// Command selected objects list
	for (i=0; i<Game.gui.selectedObjects.length; i=i+1){
		Game.gui.selectedObjects[i].goTo(tx, ty);
	}	
	//
};
